<?php

/**
 * API 抽象类
 *
 * @abstract
 * @package
 * @version $id$
 * @copyright Movoin Studio
 * @author Allen <movoin@gmail.com>
 * @license Apache license Version 2.0 {@link http://www.apache.org/licenses/LICENSE-2.0}
 */
abstract class api_abstract
{
    protected $config;
    protected $global;

    public function __construct($config=array())
    {
        $this->config = $config;
        $this->_init();
    }

    /**
     * 用于加载API的初始化参数 
     * 
     * @abstract
     * @access protected
     */
    abstract protected function _init();

    /**
     * 调用用户状态相关方法
     * 
     * @param string $actionName 
     * @param array $args
     * @abstract
     * @access public
     * @return array
     */
    abstract public function get_statuses($actionName=null, $args=array());

    /**
     * 用于设置API的参数 
     * 
     * @param string $name 
     * @param string $value 
     * @access public 
     */
    public function set_app($name, $value) {
        $options = $this->global;
        if($name) {
            $option = $this->global[$name];
            foreach($option as $key => $opt) {
                if($options[$name][$key]!=$value[$key]){
                    $this->global[$name][$key] = $value[$key];
                }
            }
        }
    }

    /**
     * 用于读取API的参数 
     * 
     * @param string $name 
     * @access public
     * @return mixed 
     */
    public function get_app($name) {
        return $this->global[$name];
    }

    /**
     * 使用CURL_EXEC 
     * 
     * @param mixed $host 
     * @access private
     * @return void
     */
    protected function exec_url($host)
    {
        $host = substr($host, strlen($host)-1, 1)=='?'?substr($host, 0, strlen($host)-1):$host;

        $ch = curl_init();
        curl_setopt( $ch, CURLOPT_URL, $host );
        curl_setopt( $ch, CURLOPT_HEADER, 0);
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
        $return = curl_exec($ch);
        $info = curl_getinfo($ch);
        curl_close($ch);
        if($info['http_code']==200)
            return $return;
        return $info['http_code'];
    }

    public function __destruct()
    {
        unset($this);
    }
}

?>
